package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.user.UserDTO;
import com.qk.entity.User;
import com.qk.management.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * &#064;author：zzq
 * &#064;createTime：2025/10/07 18:14
 * &#064;description： 
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    //根据角色查询用户,请求路径/users/role/{roleLabel}
    @GetMapping("/role/{roleLabel}")
    public Result list(@PathVariable String roleLabel) {
        log.info("根据角色查询用户，参数：{}", roleLabel);
        List<UserDO> userList = userService.listByRole(roleLabel);
        return Result.success(userList);
    }
    //修改用户
    @PutMapping
    public Result update(@RequestBody User user) {
        log.info("修改用户信息，参数：{}", user);
        userService.update(user);
        return Result.success();
    }
    //根据id查询用户
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询用户，参数：{}", id);
        UserDO user = userService.get(id);
        return Result.success(user);
    }
    //根据id批量删除用户
    @DeleteMapping("/{ids}")
    public Result deleteBatch(@PathVariable List<Integer> ids) {
        log.info("批量删除用户，参数：{}", ids);
        userService.deleteBatch(ids);
        return Result.success();
    }

    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    @GetMapping
    public Result list(UserDTO dto) {
        PageResult<UserDO> pageResult = userService.list(dto);
        return Result.success(pageResult);
    }
}
